/*******************************************************************************
 * Copyright (c) 2011 squid.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     squid - initial API and implementation
 ******************************************************************************/
package com.google.code.cubeirc.dialogs;

import lombok.Getter;
import lombok.Setter;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;

public class InputDialog extends Dialog {

	protected String result = "";
	
	protected Shell shell;
	
	private Text edtText;
	
	@Getter @Setter
	private String message;
	
	@Getter @Setter
	private String default_text;
	private Button btnCancel;

	public InputDialog(Shell parent, int style, String title, String message, String defaultText) {
		super(parent, style);
		setText(title);
		setMessage(message);
		setDefault_text(defaultText);
	}

	/**
	 * Open the dialog.
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.SYSTEM_MODAL);
		shell.setSize(334, 116);		
		shell.setText(getText());
		shell.setLayout(new FormLayout());

		Monitor primary = getParent().getMonitor();
	    Rectangle bounds = primary.getBounds();
	    Rectangle rect = shell.getBounds();
	    
	    int x = bounds.x + (bounds.width - rect.width) / 2;
	    int y = bounds.y + (bounds.height - rect.height) / 2;
	    
	    shell.setLocation(x, y);
		
		
		final Button btnOK = new Button(shell, SWT.FLAT);
		btnOK.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				result = edtText.getText();
				shell.dispose();
			}
		});
		btnOK.setImage(SWTResourceManager.getImage(InputDialog.class, "/com/google/code/cubeirc/resources/img_ok.png"));
		FormData fd_btnOK = new FormData();
		fd_btnOK.bottom = new FormAttachment(100, -4);
		fd_btnOK.right = new FormAttachment(100, -8);
		fd_btnOK.left = new FormAttachment(0, 248);
		btnOK.setLayoutData(fd_btnOK);
		btnOK.setText("OK");

		Label lblText = new Label(shell, SWT.NONE);
		FormData fd_lblText = new FormData();
		fd_lblText.top = new FormAttachment(0, 10);
		fd_lblText.left = new FormAttachment(0, 10);
		lblText.setLayoutData(fd_lblText);
		lblText.setText(getMessage());

		edtText = new Text(shell, SWT.BORDER);
		edtText.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent arg0) {
				if (arg0.keyCode == 13)
				{
					result = edtText.getText();
					shell.dispose();
				}
			}
		});
		edtText.setText(getDefault_text());
		FormData fd_edtText = new FormData();
		fd_edtText.right = new FormAttachment(0, 320);
		fd_edtText.top = new FormAttachment(lblText, 6);
		fd_edtText.left = new FormAttachment(0, 10);
		edtText.setLayoutData(fd_edtText);
		
		this.btnCancel = new Button(this.shell, SWT.NONE);
		this.btnCancel.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				shell.dispose();
			}
		});
		this.btnCancel.setImage(SWTResourceManager.getImage(InputDialog.class, "/com/google/code/cubeirc/resources/img_remove.gif"));
		FormData fd_btnCancel = new FormData();
		fd_btnCancel.bottom = new FormAttachment(100, -4);
		fd_btnCancel.left = new FormAttachment(lblText, 0, SWT.LEFT);
		this.btnCancel.setLayoutData(fd_btnCancel);
		this.btnCancel.setText("Cancel");
		edtText.setFocus();

	}
}
